package cn.iocoder.an.module.mold.controller.admin.repairjob;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.repairjob.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairjob.RepairJobDO;
import cn.iocoder.an.module.mold.service.repairjob.RepairJobService;

@Tag(name = "管理后台 - 模具维修任务")
@RestController
@RequestMapping("/mold/repair-job")
@Validated
public class RepairJobController {

    @Resource
    private RepairJobService repairJobService;

    @PostMapping("/create")
    @Operation(summary = "创建模具维修任务")
    @PreAuthorize("@ss.hasPermission('mold:repair-job:create')")
    public CommonResult<Long> createRepairJob(@Valid @RequestBody RepairJobSaveReqVO createReqVO) {
        return success(repairJobService.createRepairJob(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具维修任务")
    @PreAuthorize("@ss.hasPermission('mold:repair-job:update')")
    public CommonResult<Boolean> updateRepairJob(@Valid @RequestBody RepairJobSaveReqVO updateReqVO) {
        repairJobService.updateRepairJob(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具维修任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:repair-job:delete')")
    public CommonResult<Boolean> deleteRepairJob(@RequestParam("id") Long id) {
        repairJobService.deleteRepairJob(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具维修任务")
                @PreAuthorize("@ss.hasPermission('mold:repair-job:delete')")
    public CommonResult<Boolean> deleteRepairJobList(@RequestParam("ids") List<Long> ids) {
        repairJobService.deleteRepairJobListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具维修任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:repair-job:query')")
    @TransMethodResult
    public CommonResult<RepairJobRespVO> getRepairJob(@RequestParam("id") Long id) {
        RepairJobDO repairJob = repairJobService.getRepairJob(id);
        return success(BeanUtils.toBean(repairJob, RepairJobRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具维修任务分页")
    @PreAuthorize("@ss.hasPermission('mold:repair-job:query')")
    @TransMethodResult
    public CommonResult<PageResult<RepairJobRespVO>> getRepairJobPage(@Valid RepairJobPageReqVO pageReqVO) {
        PageResult<RepairJobDO> pageResult = repairJobService.getRepairJobPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairJobRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具维修任务 Excel")
    @PreAuthorize("@ss.hasPermission('mold:repair-job:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairJobExcel(@Valid RepairJobPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairJobDO> list = repairJobService.getRepairJobPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具维修任务.xls", "数据", RepairJobRespVO.class,
                        BeanUtils.toBean(list, RepairJobRespVO.class));
    }

    @PutMapping("/mod-status")
    @Operation(summary = "状态修改接口")
    public CommonResult<Boolean> modStatus( @Valid @RequestBody RepairJobModStatusVO modStatusVO ) {
        return success(  repairJobService.modStatus(modStatusVO));
    }

   @PutMapping("/work-report")
    @Operation(summary = "报工接口")
    public CommonResult<Boolean> workreport(@Valid @RequestBody RepairJobWorkReportVO workReportVO) {
        return success(  repairJobService.workreport(workReportVO));
    }

}